package code.hkgolden.android.activity;

import java.util.Collection;
import java.util.concurrent.Callable;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import code.framework.android.activity.ActivityConfigurator;
import code.framework.android.view.ListAdapterBase;
import code.hkgolden.android.core.HkgoldenListActivityBase;
import code.hkgolden.android.core.HkgoldenTaskManager;
import code.hkgolden.android.data.Reply;
import code.hkgolden.android.data.Topic;
import code.hkgolden.android.service.ContentService;
import code.hkgolden.android.view.NavBarViewHolder;
import code.hkgolden.android.view.ReplyViewHolder;
import code.hkgolden.android.view.TopicViewHolder;

import com.google.inject.Inject;

public class ReplyListActivity extends HkgoldenListActivityBase {

	public class ReplyListAdapter extends ListAdapterBase<Reply> {
		public ReplyListAdapter(Context c) {
			super(c);
		}

		@Override
		public boolean isEnabled(int position) {
			return false;
		}

		@Override
		public long getItemId(int position) {
			Reply reply = (Reply) super.getItem(position);
			return reply.getId();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ReplyViewHolder holder = null;
			if (convertView == null) {
				holder = ReplyListActivity.super.hkgoldenViewManager
						.createReplyViewHolder(parent, ReplyListActivity.this);
				convertView = holder.container;
				convertView.setTag(holder);
			} else {
				holder = (ReplyViewHolder) convertView.getTag();
			}
			final Reply reply = ts.get(position);
			ReplyListActivity.super.hkgoldenViewManager.renderReplyViewHolder(
					reply, holder);
			return convertView;
		}
	}

	private class ContentLoader implements Callable<Object> {
		@Override
		public Object call() throws Exception {
			logger.debug("call()");
			try {
				progressHandler.notify(HkgoldenTaskManager.REQUEST);
				final Topic topic = contentService.replies(id, (page - 1) * 30,
						30);
				progressHandler.notify(HkgoldenTaskManager.LOAD);
				ReplyListActivity.super.hkgoldenContext.sendSerializable(topic,
						topicHandler);
				final Collection<Reply> replies = topic.getReplies();
				if (replies != null && replies.size() > 0) {
					progressHandler.sendSerializableCollection(replies,
							replyHandler);
				}
			} catch (Exception e) {
				logger.error("uncaughtException", e);
			}
			return null;
		}
	}

	private class TopicHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			logger.debug("handleMessage()");
			final Bundle b = msg.getData();
			final Topic t = (Topic) b.getSerializable(Topic.class
					.getSimpleName());
			ReplyListActivity.super.hkgoldenViewManager.renderTopicViewHolder(
					t, topicViewHolder);
		}
	}

	private class ReplyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			logger.debug("handleMessage()");
			final Bundle b = msg.getData();
			final Reply r = (Reply) b.getSerializable(Reply.class
					.getSimpleName());
			replyListAdapter.add(r);
			progressHandler.notify(HkgoldenTaskManager.RENDER);
		}
	}

	int id;
	int page;

	TopicViewHolder topicViewHolder;

	ReplyListAdapter replyListAdapter;

	ContentLoader contentLoader = new ContentLoader();

	TopicHandler topicHandler = new TopicHandler();

	ReplyHandler replyHandler = new ReplyHandler();

	HkgoldenTaskManager.ProgressHandler progressHandler;

	@Inject
	ContentService contentService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		logger.debug("onCreate()");
		super.onCreate(savedInstanceState);
		
		final NavBarViewHolder navBarViewHolder = super.hkgoldenViewManager
				.createNavBarViewHolder(this, "Title");
		progressHandler = super.hkgoldenViewManager.createProgressHandler(this);
		replyListAdapter = new ReplyListAdapter(this);
		topicViewHolder = ReplyListActivity.super.hkgoldenViewManager
				.createTopicViewHolder(null, ReplyListActivity.this);
		ReplyListActivity.super.getListView().addHeaderView(
				topicViewHolder.container);
		ReplyListActivity.super.getListView().setAdapter(replyListAdapter);

		id = super.hkgoldenContext.getRequestTopicId(this);
		logger.trace("id = {}", id);
		page = super.hkgoldenContext.getRequestPage(this);
		logger.trace("page = {}", page);
		
		load();
	}
	
	@Override
	protected void load() {
		logger.debug("load()");
		replyListAdapter.clear();
		super.setProgress(0);
		ActivityConfigurator.setupDeterminateProcess(this);
		super.hkgoldenApplication.submit(contentLoader);
	}

}
